package utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Task implements Cloneable
{
	private int id;
	private String name;
	private int executionTime;
	private int priority;
	private ArrayList<Task> parents;
	
	private Map<Processor, Integer> startTime = null;
	
	public Task(int id, int executionTime, int priority, ArrayList<Task> parents)
	{
		this.id = id;
		this.executionTime = executionTime;
		this.priority = priority;
		this.parents = parents;
		this.name = "T" + id;
		
		this.startTime = new HashMap<Processor, Integer>();
	}

	public int getId() {
		return id;
	}


	public int getExecutionTime() {
		return executionTime;
	}

	
	public int getPriority() {
		return priority;
	}
	
	public ArrayList<Task> getParents() {
		return parents;
	}

	public String toString()
	{
		return "ID: " + id + " weight: " + executionTime + " priority: " + priority;
	}

	public String getName()
    {
    	return name;
    }

	public void setName(String name)
    {
    	this.name = name;
    }
	
	public Object clone() throws CloneNotSupportedException 
	{
        return super.clone();
    }

	public void setStartTime(Processor p, Integer i)
	{
		startTime.put(p, i);
	}
	
	public Integer getStartTime(Processor p)
	{
		return startTime.get(p);
	}
	
	public boolean isScheduled(Processor p)
	{
		return startTime.containsKey(p);
	}
	
	public Pair<Integer, Processor> fastestFinishingTime()
	{
		Pair<Integer, Processor> pair = new Pair<Integer, Processor>(Integer.MAX_VALUE, null);
		
		for(Processor p : startTime.keySet())
		{
			Integer ft = p.getTaskStartTime(this);
			if(ft != null && ft < pair.getFirst())
			{
				pair.setFirst(ft);
				pair.setSecond(p);
			}
		}
		
		return pair;
	}
}
